import pygame
import random

pygame.init()

# Screen dimensions
screen_width = 800
screen_height = 600

# Colors
black = (0, 0, 0)
white = (255, 255, 255)
red = (255, 0, 0)

# Snake initial position and size
snake_block = 10
snake_speed = 15
snake_list = []
snake_length = 1

# Food initial position
food_block = 10
food_x = random.randrange(0, screen_width - food_block, 10)
food_y = random.randrange(0, screen_height - food_block, 10)

# Initialize screen
screen = pygame.display.set_mode((screen_width, screen_height))
pygame.display.set_caption('Snake Game')

# Snake movement
def snake(snake_list):
    for x in snake_list:
        pygame.draw.rect(screen, black, [x[0], x[1], snake_block, snake_block])

clock = pygame.time.Clock()

game_over = False
game_close = False

# Snake initial position
x1 = screen_width / 2
y1 = screen_height / 2
x1_change = 0
y1_change = 0

while not game_over:
    while game_close == True:
        screen.fill(white)
        font = pygame.font.SysFont(None, 50)
        text = font.render('Game Over! Press Q-Quit or C-Play Again', True, red)
        screen.blit(text, (screen_width / 4, screen_height / 3))
        pygame.display.update()

        for event in pygame.event.get():
            if event.type == pygame.KEYDOWN:
                if event.key == pygame.K_q:
                    game_over = True
                    game_close = False
                if event.key == pygame.K_c:
                    x1 = screen_width / 2
                    y1 = screen_height / 2
                    x1_change = 0
                    y1_change = 0
                    snake_list = []
                    snake_length = 1
                    food_x = random.randrange(0, screen_width - food_block, 10)
                    food_y = random.randrange(0, screen_height - food_block, 10)

        for event in pygame.event.get():
            if event.type == pygame.QUIT:
                game_over = True
                game_close = False

    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            game_over = True
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                x1_change = -snake_block
                y1_change = 0
            elif event.key == pygame.K_RIGHT:
                x1_change = snake_block
                y1_change = 0
            elif event.key == pygame.K_UP:
                y1_change = -snake_block
                x1_change = 0
            elif event.key == pygame.K_DOWN:
                y1_change = snake_block
                x1_change = 0

    if x1 >= screen_width or x1 < 0 or y1 >= screen_height or y1 < 0:
        game_close = True

    x1 += x1_change
    y1 += y1_change
    screen.fill(white)
    pygame.draw.rect(screen, red, [food_x, food_y, food_block, food_block])
    snake_head = []
    snake_head.append(x1)
    snake_head.append(y1)
    snake_list.append(snake_head)
    if len(snake_list) > snake_length:
        del snake_list[0]

    for x in snake_list[:-1]:
        if x == snake_head:
            game_close = True

    snake(snake_list)

    pygame.display.update()

    if x1 == food_x and y1 == food_y:
        food_x = random.randrange(0, screen_width - food_block, 10)
        food_y = random.randrange(0, screen_height - food_block, 10)
        snake_length += 1

    clock.tick(snake_speed)

pygame.quit()
quit()